package cc.wanforme.chipmunity.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.chipmunity.common.bean.WebResponse;
import cc.wanforme.chipmunity.controller.base.BaseController;
import cc.wanforme.chipmunity.system.po.Section;
import cc.wanforme.chipmunity.system.service.interfaces.SectionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**板块控制器
 * @author wanne
 * 2019年8月17日
 * 
 */
@Controller
@RequestMapping("/section")
public class SectionController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(SectionController.class);
	
	
	@Autowired
	private SectionService sectionService;
	
	@RequestMapping("/add")
	@ResponseBody
	@ApiOperation("添加新板块")
	@ApiImplicitParam(name="section", dataType="Section", value="新板块", required=true)
	@PreAuthorize("hasAuthority('section:add')")
	public WebResponse addSection(@RequestBody(required=true) Section section) {
		try {
			return sectionService.saveSection(section);
		} catch (Exception e) {
			log.error("添加板块", e);
			return WebResponse.fail("添加失败！服务器异常！");
		}

	}
	
	@RequestMapping("/update")
	@ResponseBody
	@ApiOperation("更新新板块")
	@ApiImplicitParam(name="section", dataType="Section", value="更新id更新板块", required=true)
	@PreAuthorize("hasAuthority('section:update')")
	public WebResponse updateSection(@RequestBody(required=true) Section section) {
		try {
			return sectionService.updateService(section);
		} catch (Exception e) {
			log.error("更新模块", e);
			return WebResponse.fail("更新失败！服务器异常！");
		}
		
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@ApiOperation("删除板块")
	@ApiImplicitParam(name="section", dataType="Section", value="删除某个板块，使用id（或者用name）", required=true)
	@PreAuthorize("hasAuthority('section:delete')")
	public WebResponse deleteSection(@RequestBody(required=true) Section section) {
		try {
			return sectionService.removeSection(section);
		} catch (Exception e) {
			log.error(e.getMessage());
			return WebResponse.fail("删除失败！服务器异常！");
		}
		
	}
	
	
}
